/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.commons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.mags.remas.control.RelationshipEditorInput;

/**
 * 
 * @author Mario Gaitan
 *
 */
public final class ViewUtils {
	public static Color makeHighlightColor(Display display, Color sample) {
		int red = 0;
		int green = 0;
		int blue = 0;
		if(sample.getRed()<=220&&sample.getGreen()<=220&&sample.getBlue()<=220) {
			red = sample.getRed()+35;
			green = sample.getGreen()+35;
			blue = sample.getBlue()+35;
		} else if(sample.getRed()>=35&&sample.getGreen()>=35&&sample.getBlue()>=35) {
			red = sample.getRed()-35;
			green = sample.getGreen()-35;
			blue = sample.getBlue()-35;			
		} else if(sample.getRed()!=sample.getGreen()||sample.getRed()!=sample.getBlue()) {
			int average = (sample.getRed()+sample.getGreen()+sample.getBlue())/3;
			red = average;
			green = average;
			blue = average;	
		} else  {
			if(sample.getRed()<=220)
				red = sample.getRed()+35;
			else if(sample.getRed()>=35)
				red = sample.getRed()-35;
			if(sample.getGreen()<=220)
				green = sample.getGreen()+35;
			else if(sample.getGreen()>=35)
				green = sample.getGreen()-35;
			if(sample.getBlue()<=220)
				blue = sample.getBlue()+35;
			else if(sample.getBlue()>=35)
				blue = sample.getBlue()-35;				
		}
		return new Color(display, red, green, blue);
	}
	public static IWorkbenchPart openOrBringToTopEditor(RelationshipEditorInput input, String ID, IWorkbenchPartSite site) {
		try {
			IWorkbenchPart editorPart = getEditorReference(input, ID, site);
			if(editorPart==null) 
				return site.getPage().openEditor(input, ID);
			else  {
				site.getPage().bringToTop(editorPart);
				return editorPart;
			}
		} catch(Exception e) {
			if(e instanceof RuntimeException)
				throw (RuntimeException)e;
			else
				throw new RuntimeException(e);
		}
	}
	public static IWorkbenchPart getEditorReference(RelationshipEditorInput input, String ID, IWorkbenchPartSite site) {
		try {
			IEditorReference[] editorsReference = site.getPage().findEditors(input, ID, SWT.BORDER);					
			IEditorReference editorReference = null;
			for(IEditorReference i: editorsReference)
				if(i.getEditorInput() instanceof RelationshipEditorInput) {
					if(i.getEditorInput()!=null
					&& ((RelationshipEditorInput)i.getEditorInput()).getEntityRelationship().getEntityRelationshipId()==input.getEntityRelationship().getEntityRelationshipId()
					&& ((RelationshipEditorInput)i.getEditorInput()).getClassName().equalsIgnoreCase(input.getClassName())
					&& ((RelationshipEditorInput)i.getEditorInput()).getID()==input.getID()) {
						editorReference = i;
						break;
					}
				}
			return (editorReference==null)?null:editorReference.getPart(true);
		} catch(Exception e) {
			if(e instanceof RuntimeException)
				throw (RuntimeException)e;
			else
				throw new RuntimeException(e);
		}		
	}
	public static IWorkbenchPart bringToTopEditor(RelationshipEditorInput input, String ID, IWorkbenchPartSite site) {
		try {
			IWorkbenchPart editorPart = getEditorReference(input, ID, site);
			if(editorPart!=null) {
				site.getPage().bringToTop(editorPart);
				return editorPart;
			} else
				return null;
		} catch(Exception e) {
			if(e instanceof RuntimeException)
				throw (RuntimeException)e;
			else
				throw new RuntimeException(e);
		}
	}	
}
